package com.eascs.admin.o2o.loan.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.detail.entity.OaUnit;
import com.eascs.admin.o2o.detail.service.OaLoanapplyDetailService;
import com.eascs.admin.o2o.loan.entity.LoanApplyFileEntity;
import com.eascs.admin.o2o.loan.entity.O2oBankTransLogFrom;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.oper.service.IO2oOperationUploanlogService;
import com.eascs.admin.o2o.recommend.entity.O2oRecommendEntity;
import com.eascs.admin.o2o.recommend.service.O2oRecommendService;
import com.eascs.app.dao.PageResult;
import com.eascs.brand.service.O2oPartnerService;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.crm.common.entity.CustomerBasicInfoEntity;
import com.eascs.crm.common.entity.CustomerUnit;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.crm.common.vo.CustomerView;
import com.eascs.crm.common.vo.QueryCustomerVO;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.service.wf.WorkFlowService;

@Controller
@RequestMapping("/loanapply")
public class O2oLoanapplyController extends BaseMasterViewController {
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;

	@Reference
	private WorkFlowService workFlowService;

	@Reference
	ICustomerInfoService iCustomerInfoService;
	@Reference
	IO2oOperationUploanlogService iO2oOperationUploanlogService;

	@Reference
	private SysOrgService sysOrgService;

	@Autowired
	private O2oDataListService o2oDataListService;
	
	@Autowired
	O2oPartnerService partnerService;
	
	@Autowired
	private OaLoanapplyDetailService oaLoanapplyDetailService;

	@Autowired
	private O2oLoanUserService o2oLoanUserService;

	@Autowired
	O2oRecommendService o2oRecommendService;
	
	@Reference
	CustomerService customerService;
	
	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "loanManage1");
	}

	public static boolean isNum(String str) {
		return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
	}
	/**
	 * 新增终端贷和续贷终端贷
	 * @param id
	 * @param billCode
	 * @param loanType
	 * @return
	 */
	@RequestMapping(value="/addloan")
	public String toAddloanapply(String customerId,String billCode,String loanType,String codeType){
		LoanApplyFileEntity o2oLoanapplyEntity = new LoanApplyFileEntity();
		o2oLoanapplyEntity.setLoanType(loanType);
		o2oLoanapplyEntity.setBillCode(billCode);
		o2oLoanapplyEntity.setCodeType(codeType);
		o2oLoanapplyEntity.setContinueLoanParenNo(billCode);
		CustomerEntity customer = customerService.queryCustomerEntityByCustomerId(customerId);
		if (customer != null) {
			O2oRecommendEntity entity = o2oRecommendService.findEntityByCard(customer.getIdNumber());
			if(entity!=null){
				User user = UserHolder.getUser();
				if (entity.getOperatorNo().equals(user.getNo())) {
					ViewContext.put("recommendType", entity.getRecommendType());
					ViewContext.put("referrerName", entity.getReferrerName());
					ViewContext.put("referrerNo", entity.getReferrerNo());
				}
			}
		}
		ViewContext.put("loanApplyFileEntity", o2oLoanapplyEntity);
		ViewContext.put("customerId", customerId);
		return "loanManagement/addloanapply";
	}
	/**
	 * 接力贷和分期贷
	 * @param id
	 * @param billCode
	 * @param loanType
	 * @return
	 */
	@RequestMapping(value="/continueloan")
	public String continueApply(String customerId,String codeType,String billCode,String loanType,String continueLoanParenNo,String titleName){
		QueryCustomerVO vo = new QueryCustomerVO();
		vo.setCustomer_id(customerId);
		CustomerView customer = iCustomerInfoService.geCustomerView(vo);
		CustomerBasicInfoEntity basicEntity = customer.getBasicInfo();
		CustomerBasicInfoEntity spouseEntity = null;
		LoanApplyFileEntity o2oLoanapplyEntity = new LoanApplyFileEntity();
		o2oLoanapplyEntity.setLoanType(loanType);
		o2oLoanapplyEntity.setBillCode(billCode);
		o2oLoanapplyEntity.setContinueLoanParenNo(continueLoanParenNo);
		o2oLoanapplyEntity.setTitleName(titleName);
		o2oLoanapplyEntity.setCodeType(codeType);
		List<CustomerUnit> custmenrList = iCustomerInfoService.findCustomerUnit(basicEntity.getCustomerId());
		if (custmenrList != null && !custmenrList.isEmpty()) {
			o2oLoanapplyEntity.setLtd(custmenrList.get(0).getUnitId());
			o2oLoanapplyEntity.setLtdCode(custmenrList.get(0).getUnitCode());
			o2oLoanapplyEntity.setLtdName(custmenrList.get(0).getUnitName());
			OaUnit oaUnit=oaLoanapplyDetailService.findUnit(custmenrList.get(0).getUnitId());
			if(null!=oaUnit){
				o2oLoanapplyEntity.setBuybackId(oaUnit.getUnitNo());
				o2oLoanapplyEntity.setBuybackCode(oaUnit.getUnitCode());
				o2oLoanapplyEntity.setBuybackName(oaUnit.getNamecn());
			}
		} 
		ViewContext.put("basic", basicEntity);
		ViewContext.put("expan", customer.getExpansionInfo());
		ViewContext.put("loanApplyFileEntity", o2oLoanapplyEntity);
		if (customer != null && customer.getBasicInfo() != null && customer.getBasicInfo().getMateId() != null) {
			QueryCustomerVO vo2 = new QueryCustomerVO();
			vo2.setCustomer_id(customer.getBasicInfo().getMateId());
			CustomerView customer1 = iCustomerInfoService.geCustomerView(vo2);
			spouseEntity = customer1.getBasicInfo();
			ViewContext.put("materBasic", spouseEntity);
			ViewContext.put("materExpan", customer1.getExpansionInfo());
		} 
		return "loanManagement/continueLoanapply";
	}
	/**
	 * 修改详情页面
	 * 
	 * @param loanapplyNo
	 * @param massege
	 * @return
	 */
	@RequestMapping(value = "/findloan")
	public String loanfile(String loanapplyNo, String massege) {
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
		/**** 查询附件 ****/
		List<O2oDataListEntity> o2oDataList = o2oDataListService.findByEntityList(loanapplyNo);
		O2oLoanUserEntity  customer =  o2oLoanUserService.findById(o2oLoanapplyEntity.getLoanId(),loanapplyNo);
		O2oLoanUserEntity customerMate =  o2oLoanUserService.findSpouseId(o2oLoanapplyEntity.getLoanId(),loanapplyNo);
		if( customerMate!=null){
			if (customer != null && customerMate != null) {
				ViewContext.put("materBasic", customerMate);
			}
		}
//		if((StatusContexts.LOAN_TYPE_BP.equals(o2oLoanapplyEntity.getLoanType())||StatusContexts.LOAN_TYPE_BP_JX.equals(o2oLoanapplyEntity.getLoanType())||StatusContexts.LOAN_TYPE_SG.equals(o2oLoanapplyEntity.getLoanType())||StatusContexts.LOAN_TYPE_YC.equals(o2oLoanapplyEntity.getLoanType())||StatusContexts.LOAN_TYPE_SC.equals(o2oLoanapplyEntity.getLoanType()))&&!StringUtil.isEmpty(o2oLoanapplyEntity.getPartnerId())){
//			String partnerName = partnerService.getPartnerBrandName(o2oLoanapplyEntity.getPartnerId());
//			ViewContext.put("partnerName", partnerName);
//		}
		ViewContext.put("o2oDataList", o2oDataList);
		ViewContext.put("errorMassege", massege);
		ViewContext.put("loanApplyFileEntity", o2oLoanapplyEntity);
		return "loanManagement/loanfile";
	}

	/**
	 * 中行接口交互信息列表展示
	 * 
	 * @param bankTransLog
	 *            查询实体
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/bklog")
	public String findBocLog(O2oBankTransLogFrom bankTransLogFrom) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "bankCodeInfo");
		bankTransLogFrom.setPageSize(10);
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oBankByPage(bankTransLogFrom);
		String[] oatitles = { "申请单号", "贷款单号", "银行类型", "协议名称", "处理结果", "日志信息", "开始时间", "结束时间" };
		SelectDataOption[] selectDataOptions = { null, null, new SelectDataOption("BANKTYPEO2O"), null, new SelectDataOption("BANK_HANDLE_STATUS"), null, null, null };
		String oapageUrl = "/loanapply/bklog";
		if (bankTransLogFrom != null) {
			String loanApplyNo = bankTransLogFrom.getLoanApplyNo() == null ? "" : bankTransLogFrom.getLoanApplyNo();
			String bankType = bankTransLogFrom.getBankType() == null ? "" : bankTransLogFrom.getBankType();
			String isSuccess = bankTransLogFrom.getIsSuccess() == null ? "" : bankTransLogFrom.getIsSuccess();
			oapageUrl = UrlUtil.appendUrl(oapageUrl.toString(), "loanApplyNo", loanApplyNo);
			oapageUrl = UrlUtil.appendUrl(oapageUrl, "bankType", bankType);
			oapageUrl = UrlUtil.appendUrl(oapageUrl, "isSuccess", isSuccess);
			oapageUrl = oapageUrl + "&pageIndex=";
		} else {
			oapageUrl = oapageUrl + "?pageIndex=";
		}
		PaginationInfo oapaginationInfo = new PaginationInfo(pageResult, oatitles, oapageUrl);
		oapaginationInfo.setBuildPagingNavigation(true);
		oapaginationInfo.setFirstRenderColumnIndex(0);
		oapaginationInfo.setSelectDataOptions(selectDataOptions);
		ViewContext.put("oapaginationInfo", oapaginationInfo);
		ViewContext.put("vo", bankTransLogFrom);
		return "/loanManagement/bklog";
	}
}
